
import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import { getEnv, regExps } from './config'
import { composePlugins } from './config/plugins'

function resolve( dir ) {
  return path.join( __dirname, dir )
}

// https://vitejs.dev/config/
export default defineConfig( ( { command, mode } ) => {
  const root = process.cwd()
  const env = getEnv( loadEnv( mode, process.cwd() ) )
  const { VITE_PORT, VITE_BASE_API, VITE_BASE_PATH, VITE_SERVER_PORT, VITE_LEGACY } = env

  const define = {
    // https://vue-i18n.intlify.dev/guide/advanced/optimization.html#quasar-cli
    // 消除 vue-i18n 警告
    __VUE_I18N_FULL_INSTALL__ : true,
    __VUE_I18N_LEGACY_API__ : false,
    __INTLIFY_PROD_DEVTOOLS__ : false,
    __APP_INFO__ : JSON.stringify( {
      version : '3.0.0'
    } )
  }

  const rollupOptions = {
    output : {
      manualChunks( id ) {
        if ( id.includes( 'node_modules' ) ) {
          return id
            .toString()
            .split( 'node_modules/' )[1]
            .split( '/' )[0]
            .toString()
        }
      },
      chunkFileNames : ( chunkInfo ) => {
        const facadeModuleId = chunkInfo.facadeModuleId
          ? chunkInfo.facadeModuleId.split( '/' )
          : []
        const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]'
        return `js/${fileName}/[name].[hash].js`
      }
    }
  }
  return {
    root,
    base : './', //
    resolve : {
      alias : {
        '@' : resolve( 'src' )
      },
      extensions : ['.js', '.json', '.ts', '.vue']
    },

    plugins : composePlugins( command, VITE_LEGACY ),

    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/styles/element/index.scss" as *;`,
        }
      }
    },

    server : {
      host : '0.0.0.0',
      port : VITE_PORT || 9527,
      https : false,
      open : false,
      proxy : {
        [VITE_BASE_API] : {
          target : `${VITE_BASE_PATH}:${VITE_SERVER_PORT}/`, // 代理到 目标路径
          changeOrigin : true,
          rewrite : ( path ) => regExps( path, VITE_BASE_API )
        }
      }
    },

    define,

    build : {
      path : './',
      sourcemap : false,
      brotliSize : false,
      chunkSizeWarningLimit : 2500,

      // minify: 'terser',
      // terserOptions: {
      //   compress: {
      //     drop_console: true,
      //     drop_debugger: true
      //   }
      // },

      rollupOptions
    }
  }
} )
